package com.thread.wait;

/**
 * @author wangyingjie
 * @date
 * @remark
 */
public class ChangeThread extends Thread {

    //挂起标识
    private volatile Boolean isSuspend = false;

    @Override
    public void run() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        synchronized (this){
            System.out.println("进t1同步了。。");
            if (isSuspend){
                try {
                    System.out.println("被挂起了。。。");
                    wait();
                    System.out.println("被唤醒了。。。");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        System.out.println("t1同步结束了。。。");
    }

    /**
     * 挂起
     */
    private void suSpendMe(){
        isSuspend = true;
    }

    /**
     * 唤醒
     */
    private void resumeMe(){
        isSuspend = false;
        synchronized (this){
            this.notify();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        ChangeThread t1 = new ChangeThread();
        Thread t2 = new Thread(){
            @Override
            public void run() {
                System.out.println("t2....");
            }
        };
        t1.start();
        t2.start();
        t1.suSpendMe();
        Thread.sleep(2000);
        t1.resumeMe();
    }
}
